﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace CoreProvince
{
    public class ProvinceBll
    {
        public static int Insert(nlit_Province Info, string strConnect)
        {
            int id = 0;
            using (ProvinceToLinQDataContext db = new ProvinceToLinQDataContext(strConnect))
            {
                try
                {
                    db.nlit_Provinces.InsertOnSubmit(Info);
                    db.SubmitChanges();
                    id = Info.Id;
                }
                catch { id = 0; }
                finally { db.Dispose(); }
            }
            return id;
        }

        public static bool Update(nlit_Province Info, string strConnect)
        {
            bool res = false;
            using (ProvinceToLinQDataContext db = new ProvinceToLinQDataContext(strConnect))
            {
                try
                {
                    nlit_Province o = db.nlit_Provinces.Single(p => p.Id == Info.Id);
                    if (o != null)
                    {
                        o.Name = Info.Name;
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static bool UpdateOrderDisplay(int id, int orderDisplay, string strConnect)
        {
            bool res = false;
            using (ProvinceToLinQDataContext db = new ProvinceToLinQDataContext(strConnect))
            {
                try
                {
                    nlit_Province o = db.nlit_Provinces.Single(p => p.Id == id);
                    if (o != null)
                    {
                        o.OrderDisplay = orderDisplay;
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static bool UpdateIsDeleted(int id, bool isDeleted, string strConnect)
        {
            bool res = false;
            using (ProvinceToLinQDataContext db = new ProvinceToLinQDataContext(strConnect))
            {
                try
                {
                    nlit_Province o = db.nlit_Provinces.Single(p => p.Id == id);
                    if (o != null)
                    {
                        o.IsDeleted = isDeleted;
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static bool UpdateIsActive(int id, bool isActive, string strConnect)
        {
            bool res = false;
            using (ProvinceToLinQDataContext db = new ProvinceToLinQDataContext(strConnect))
            {
                try
                {
                    nlit_Province o = db.nlit_Provinces.Single(p => p.Id == id);
                    if (o != null)
                    {
                        o.IsActive = isActive;
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static List<nlit_Province> GetList(bool isDeleted, bool isActive, string strConnect)
        {
            List<nlit_Province> List = new List<nlit_Province>();

            using (ProvinceToLinQDataContext db = new ProvinceToLinQDataContext(strConnect))
            {
                try
                {
                    var varlist = from p in db.nlit_Provinces
                                  orderby p.OrderDisplay
                                  where p.IsDeleted == isDeleted && p.IsActive == isActive
                                  select p;
                    List = varlist.ToList();
                }
                catch
                {
                    List = null;
                }
                finally
                {
                    db.Dispose();
                }
            }
            return List;
        }

        public static List<nlit_Province> GetList(string strConnect)
        {
            List<nlit_Province> List = new List<nlit_Province>();

            using (ProvinceToLinQDataContext db = new ProvinceToLinQDataContext(strConnect))
            {
                try
                {
                    var varlist = from p in db.nlit_Provinces
                                  where p.IsDeleted==false
                                  orderby p.Name ascending, p.OrderDisplay descending
                                  select p;
                    List = varlist.ToList();
                }
                catch
                {
                    List = null;
                }
                finally
                {
                    db.Dispose();
                }
            }
            return List;
        }
        public static List<nlit_Province> GetList(bool isActive, string strConnect)
        {
            List<nlit_Province> List = new List<nlit_Province>();

            using (ProvinceToLinQDataContext db = new ProvinceToLinQDataContext(strConnect))
            {
                try
                {
                    var varlist = from p in db.nlit_Provinces
                                  where p.IsDeleted == false && p.IsActive == isActive
                                  orderby p.Name ascending, p.OrderDisplay descending
                                  select p;
                    List = varlist.ToList();
                }
                catch
                {
                    List = null;
                }
                finally
                {
                    db.Dispose();
                }
            }
            return List;
        }
        public static nlit_Province GetById(int id, string strConnect)
        {
            nlit_Province res = new nlit_Province();

            using (ProvinceToLinQDataContext db = new ProvinceToLinQDataContext(strConnect))
            {
                try
                {

                    res = db.nlit_Provinces.Single(p => p.Id == id);

                }
                catch
                {

                }
                finally
                {
                    db.Dispose();
                }
            }
            return res;
        }
    }
}
